﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.MaHaiBo.API.DTOS;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.Entity.Traceability;
using RBAC.MaHaiBo.Service.TraceabilityService;
using MediatR;
using RBAC.MaHaiBo.API.Application.Command.TracingCommand;

namespace RBAC.MaHaiBo.API.Controllers
{
    /// <summary>
    ///溯源控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TracingController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<TracingController> logger;
        /// <summary>
        /// 溯源服务层
        /// </summary>
        ITracingService TSer;
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="TSer">溯源服务层param>
        public TracingController(ILogger<TracingController> logger,
        ITracingService TSer,
             IMediator mediator)
        {
            this.logger = logger;
            this.TSer = TSer;
            this.mediator = mediator;
        }
        /// <summary>
        /// 显示商品名称
        /// </summary>
        /// <returns>返回商品列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<Commodity>>> GetCommodities()
        {
            ApiResult<List<Commodity>> result = new ApiResult<List<Commodity>>();
            try
            {
                result.Data = await mediator.Send(new CommodityShowCommand());
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【溯源服务层】显示商品名称 出错");
                throw;
            }
            return result;
        }

        /// <summary>
        /// 添加溯源
        /// </summary>
        /// <param name="tracing">要添加的信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddTracing(TracingCreateCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【溯源服务层】添加溯源 出错");
                throw;
            }
            return result;
        }

        /// <summary>
        /// 显示溯源列表信息
        /// </summary>
        /// <param name="pageiden">页码</param>
        /// <param name="pageSzen">页容量</param>
        /// <param name="CommodityName">商品名称</param>
        /// <returns>返回数据信息</returns>
        [HttpGet]
        public async Task<ApiResult<FenYeDTO<TracingDTO>>> GetTracing([FromQuery]TracingShowCommand request)
        {
            ApiResult<FenYeDTO<TracingDTO>> result = new ApiResult<FenYeDTO<TracingDTO>>();
            try
            {
                result.Data = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【溯源服务层】显示溯源列表信息 出错");
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改溯源
        /// </summary>
        /// <param name="tracing">效果后的溯源信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdTracing( TracingUpdateCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【溯源服务层】修改溯源 出错");
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除溯源信息
        /// </summary>
        /// <param name="Ids">要删除的数据信息集合</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> DelTracing(TracingDeleteCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【溯源服务层】修改溯源 出错");
                throw;
            }
            return result;
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UploadFile(IFormFile file)
        {
            try
            {
                string savepath = Directory.GetCurrentDirectory() + @"\wwwroot\pic\";
                string nweFilename=Guid.NewGuid().ToString()+Path.GetExtension(file.FileName);
                using (FileStream fs = new FileStream(savepath+ nweFilename, FileMode.Create))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                return Ok(nweFilename);
            }
            catch (Exception ex)
            {

                logger.LogError("【溯源服务层】上传图片 出错");
                throw;
            }
        }
        /// <summary>
        /// 反填数据信息
        /// </summary>
        /// <param name="Tracingid">编号</param>
        /// <returns>返回数据信息</returns>
        [HttpGet]
        public async Task<ApiResult<List<TracingDTO>>> Tracinginfo([FromQuery]TracingFanTianCommand request)
        {
            ApiResult<List<TracingDTO>> result = new ApiResult<List<TracingDTO>>();
            try
            {
                result.Data = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【溯源服务层】修改溯源 出错");
                throw;
            }
            return result;
        }

    }
}
